package com.tbit.uqbike.service.business.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tbit.uqbike.object.pojo.User;
import com.tbit.uqbike.object.pojo.UserRidingScoreConfig;
import com.tbit.uqbike.object.pojo.UserRidingScoreLog;
import com.tbit.uqbike.object.pojo.dto.UserRidingScoreModifyDTO;
import com.tbit.common.entity.view.PageResult;
import com.tbit.common.entity.view.Result;
import com.tbit.uqbike.service.business.UserRidingScoreConfigService;
import com.tbit.uqbike.webmanager.dao.core.UserDao;
import com.tbit.uqbike.webmanager.dao.core.UserRidingScoreConfigDao;
import com.tbit.uqbike.webmanager.dao.log.UserRidingScoreLogDao;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Objects;
import java.util.Optional;

/**
 * @author yee_
 * @version 1.0
 * @date 2023/2/10 13:34
 */
@Service
public class UserRidingScoreConfigServiceImpl implements UserRidingScoreConfigService {
    @Resource
    private UserRidingScoreConfigDao userRidingScoreConfigDao;
    @Resource
    private UserRidingScoreLogDao userRidingScoreLogDao;
    @Resource
    private UserDao userDao;

    @Override
    public void create(UserRidingScoreConfig entity) {
        userRidingScoreConfigDao.insert(entity);
    }

    @Override
    public void update(UserRidingScoreConfig entity) {
        UserRidingScoreConfig config = userRidingScoreConfigDao.selectById(entity.getAccountId());
        if (config == null) {
            create(entity);
            return;
        }
        userRidingScoreConfigDao.updateById(entity);
    }

    @Override
    public Result<UserRidingScoreConfig> findConfigByAccountId(Integer accountId) {
        UserRidingScoreConfig config = userRidingScoreConfigDao.selectById(accountId);
        if (config == null) {
            return Result.success(new UserRidingScoreConfig(accountId));
        }
        return Result.success(config);
    }

    @Override
    public Result<PageResult<UserRidingScoreLog>> listRidingScoreLogByUserId(Integer userId, Page<UserRidingScoreLog> page) {
        LambdaQueryWrapper<UserRidingScoreLog> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserRidingScoreLog::getUserId, userId);
        queryWrapper.orderBy(true, false, UserRidingScoreLog::getUpdateTime);
        IPage<UserRidingScoreLog> pageResult = userRidingScoreLogDao.selectPage(page, queryWrapper);
        return Result.success(new PageResult<>((int) page.getCurrent(), (int) page.getSize(), pageResult.getTotal(),
                (int) pageResult.getPages(), pageResult.getRecords()));
    }

    @Override
    public Result<String> modifyUserRidingScore(UserRidingScoreModifyDTO modifyDTO) {
        User user = userDao.getByUserId(modifyDTO.getUserId());
        if (Objects.isNull(user)) {
            return Result.error("用户查询为空");
        }
        Integer modifyScore =
                modifyDTO.getIsAdd() ? modifyDTO.getModifyScore() : Math.negateExact(modifyDTO.getModifyScore());
        int afterValue = user.getRidingScore() + modifyScore;
        int maxVal = 600;
        int minVal = 0;
        if (afterValue > maxVal) {
            return Result.error("信用分值最大为600分,剩余可加" + (maxVal - user.getRidingScore()) + "分");
        }
        if (afterValue < minVal) {
            return Result.error("信用分值最低为0分,剩余可减" + user.getRidingScore() + "分");
        }
        UserRidingScoreLog newLog = new UserRidingScoreLog();
        newLog.setUserId(modifyDTO.getUserId())
                //人工修改为-1
                .setType(-1)
                .setBeforeScore(user.getRidingScore())
                .setScore(modifyScore)
                .setAfterScore(afterValue)
                .setUpdateTime(new Date())
                .setOperationId(modifyDTO.getOperationId())
                .setOperationPhone(modifyDTO.getOperationPhone())
                .setRemark(modifyDTO.getRemark());
        //增加日志并修改分数
        userRidingScoreLogDao.insert(newLog);
        userDao.updateUserRidingScore(modifyDTO.getUserId(), modifyScore);
        return Result.success();
    }

    @Override
    public Result<Integer> getByRidingScoreUserId(Integer userId) {
        Integer ridingScore = Optional.ofNullable(userDao.getByUserId(userId)).map(User::getRidingScore).orElse(0);
        return Result.success(ridingScore);
    }

}
